package com.zja.www.dao.impl;

import com.zja.www.entity.User;
import com.zja.www.dao.UserDao;
import com.zja.www.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;




/**
 * @author ja
 * @create 2025/3/23
 * @description User实体类dao层接口实现类
 */
public class UserDaoImpl implements UserDao {
    private final Connection connection;

    /**
     * 构造函数初始化数据库
     */
    public UserDaoImpl() {
        this.connection = DBUtil.getConnection();
        if (this.connection == null) {
            throw new RuntimeException("无法获取数据库连接");
        }

    }


    @Override
    public boolean login(String userName, String password) {
        // SQL 查询语句
        String sql = "SELECT * FROM user WHERE username = ? AND password = ?";

        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, userName);
            ps.setString(2, password);
            try (ResultSet rs = ps.executeQuery()) {
                return rs.next();
            }
        } catch (SQLException e) {
            // 打印异常信息（建议使用日志框架，如 SLF4J）
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean insert(User user) {
        String sql = "INSERT INTO user(username, password,student_id,phone) VALUES(?, ?, ?, ?)";
        // 进行数据库插入操作
        try (PreparedStatement ps = connection.prepareStatement(sql)) {

            ps.setString(1, user.getUserName());
            ps.setString(2, user.getPassword());
            ps.setInt(3, user.getStudentId());
            ps.setString(4, user.getPhone());


            int rowsAffcted = ps.executeUpdate(); //执行插入操作，并返回受影响的行数

            return rowsAffcted > 0;
        } catch (SQLException e) {
            //打印异常信息
            e.printStackTrace();
        }
        return false;
    }


    @Override
    public User getUserById(int id) {
        // 进行数据库查询操作
        String sql = "SELECT * FROM user WHERE id = ?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, id);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    User user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUserName(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setStudentId(rs.getInt("student_id"));
                    user.setPhone(rs.getString("phone"));
                    user.setStatus(rs.getString("status"));
                    return user;
                }
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public List<User> getAllUser() {
        // 进行数据库查询操作
        List<User> users = new ArrayList<>();
        String sql = "SELECT * FROM user";
        try (PreparedStatement ps = connection.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUserName(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setStudentId(rs.getInt("student_id"));
                user.setPhone(rs.getString("phone"));
                user.setStatus(rs.getString("status"));
                users.add(user);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    @Override
    public boolean updateUser(User user) {
        String sql = "update user set username = ?, password = ?, student_id = ? ,phone = ? ,status = ? where id = ?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getPassword());
            ps.setInt(3, user.getStudentId());
            ps.setString(4, user.getPhone());
            ps.setString(5, user.getStatus());
            ps.setInt(6, user.getId());
            int rowsAffcted = ps.executeUpdate();
            return rowsAffcted > 0;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteUser(int id) {
        String sql = "delete from user where id = ?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, id);
            int rowsAffcted = ps.executeUpdate();
            return rowsAffcted > 0;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public User getUserByUsername(String username) {
        String sql = "SELECT * FROM user WHERE username = ?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUserName(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setStudentId(rs.getInt("student_id"));
                user.setPhone(rs.getString("phone"));
                user.setStatus(rs.getString("status"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> getUsersByStatus(String status) {
        List<User> users = new ArrayList<>();
        String sql = "SELECT * FROM user WHERE status = ?";

        try (PreparedStatement ps = connection.prepareStatement(sql)) {

            ps.setString(1, status);
            ResultSet rs = ps.executeQuery();


            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUserName(rs.getString("username"));
                user.setStatus(rs.getString("status"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

}
